<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------

namespace app\home\controller;

use service\LogService;
use service\NodeService;
use think\Db;
use think\Request;
use think\Validate;
use think\Controller;
use app\common\tools\Arr;

/**
 * 登录
 * Class Index
 * @package app\home\controller
 * @author pp <124910168@qq.com>
 * @date 2020/08/15 10:41
 */
class Login extends Controller
{

    public $user;

    /**
     * 控制器基础方法
     */
    public function _initialize()
    {
        // halt(session('member'));
        parent::_initialize();
        $this->user = Db::connect(config('dbx.SU'))->name('Member');
    }

    public function index()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            // halt($post);
            $username = trim($post['username']);
            $password = trim($post['password']);
            if (strlen($username) < 6) {
                return ['status' => 0, 'msg' => '账号长度不能少于6位有效字符', 'url' => url('@home/login')];
            }
            if (strlen($password) < 6) {
                return ['status' => 0, 'msg' => '密码长度不能少于6位有效字符', 'url' => url('@home/login')];
            }

            // 验证码
            $verifyCode = $this->request->post('code', '', 'trim');
            if( !captcha_check($verifyCode) ) {
                $this->error('验证码不正确');
            }

            // 用户信息验证
            $user = $this->user->where(['username|phone|mail' => $username])->find();
            if (empty($user)) {
                return ['status' => 0, 'msg' => '登录账号不存在，请重新输入', 'url' => url('@home/login')];
            }
            if (($user['password'] !== md5($password))) {
                return ['status' => 0, 'msg' => '密码错误，请重新输入', 'url' => url('@home/login')];
            }
            if ($user['status'] == 0) {
                return ['status' => 0, 'msg' => '账号已经被禁用，请联系管理', 'url' => url('@home/login')];
            }
            if ($user['is_deleted'] == 1) {
                return ['status' => 0, 'msg' => '账号已经被删除，请联系管理', 'url' => url('@home/login')];
            }
            
            // 更新用户信息
           $data = ['login_at' => time(), 'login_num' => ['exp', 'login_num+1']];
            $this->user->where(['id' => $user['id']])->update($data);
            session('uid', $user['id']);
            session('username', $user['username']);
            session('member', $user);
            return ['status' => 1, 'msg' => '登录成功，正在进入系统', 'url' => url('@home/member.index/index')];
        }else{
            return $this->fetch('index', ['title' => '用户登录']);
        }
    }

    public function verify_code(){
		ob_clean();
        if (is_mobile()){
            $array = array(
                'imageW' => 80,
                'imageH' => 39,
                'length' => 4,
                'fontSize'=>12,
                // 是否画混淆曲线
                'useNoise' => false,
                'useCurve' => false,
            );
        }else{
            $array = array(
                'imageW' => 130,
                'imageH' => 38,
                'length' => 4,
                'fontSize'=>16,
                // 是否画混淆曲线
                'useNoise' => false,
                'useCurve' => false,
            );
        }


        return captcha("", $array);
    }

    //退出登录
    public function out(){
        session('uid',null);
        session('username',null);
        session('member',null);
        $this->redirect('@home');
    }
}
